/**
 * Created by John on 2016/7/8.
 */

function settings() {
    myDB.runCommand({
        getLastError: 1,
        w: 1,
        J: true,
        wtimeout: 1000
    });
}

function getError() {
    var mongo = new Mongo('localhost');
    var wordsDB = mongo.getDB('words');
    var wordsColl = wordsDB.getCollection('word_stats');
    wordsColl.insert({word: 'the'});
    var lastError = wordsDB.getLastError();
    if (lastError) {
        print("ERROR: " + lastError);
    }
}

function errorCommand() {
    var mongo = new Mongo('localhost');
    var wordsDB = mongo.getDB('words');
    wordsDB.runCommand({getLastError: 1, w: 1, j: true, wtimeout: 1000});
    var wordsColl = wordsDB.getCollection('word_stats');
    wordsColl.inset({word: 'the'});
    var results = wordsDB.runCommand({getLastError: 1});
}

function row() {
    var row = {
        name: 'myName',
        countA: 0,
        countB: 0,
        days: ['Monday', 'Wednesday'],
        scores: [{id: 'test1', score: 94},
            {id: 'test2', score: 85},
            {id: 'test3', score: 97}]
    }
}

function set() {
    var set = {
        $inc: {countA: 5, countB: 1},
        $set: {name: 'New name'},
        $push: {days: 'Friday'},
        $sort: {score: 1}
    }
}

function add() {
    var mongo = new Mongo('localhost');
    var myDB = mongo.getDB('myDB');
    var myColl = myDB.getCollection('myCollection');
    myColl.insert({color: 'blue', value: 7, name: 'Ordan'});
}

function update() {
    update({category: 'new'}, {$set: {category: 'old'}}, false, true);
    update({category: 'new', $isolated: 1}, {$set: {category: 'old'}}, false, true);
}

function save() {
    existingObject = myCollection.findOne({name: 'existingObj'});
    existingObject.name = 'updateObj';
    myCollection.save(existingObject);
    myCollection.save({name: 'newObj'});
}

function upsert() {
    update({color: 'azure'}, {$set: {red: 0, green: 127, blue: 255}}, true, false);
}

function remove() {
    collection = myDB.getCollection('word_stats');
    collection.remove();
}

function removeA() {
    collection = myDB.getCollection('word_stats');
    collection.remove({first: 'a'}, false);
}

function removeOneA() {
    collection = myDB.getCollection('word_stats');
    collection.remove({first: 'a'}, true);
}


